package com.lant.web.controller.user;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.lant.enums.ResponseStatusEnum;
import com.lant.pojo.LantResult;
import com.lant.utils.ObjectUtils;
import com.lant.utils.RandomValidateCodeUtil;
import com.lant.web.ao.SMSVerifyAO;
import com.lant.web.constant.KEYConstant;

@RestController
public class VerifyController {
    private final static Logger logger = LoggerFactory.getLogger(VerifyController.class);

    @Autowired
    private SMSVerifyAO smsVerifyAO;

    /**
     * 生成验证码
     */
    @RequestMapping(value = "/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandcode(request, response);// 输出验证码图片方法
        } catch (Exception e) {
            logger.error("===获取验证码失败>>>>   ", e);
        }
    }

    /**
     * 校验验证码
     */
    @RequestMapping(value = "/checkVerify", method = RequestMethod.POST)
    public LantResult checkVerify(String vercode, HttpSession session) {
        try {
            // 从session中获取随机数
            String inputStr = vercode;
            String random = (String) session.getAttribute(KEYConstant.RANDOMCODEKEY);
            if (ObjectUtils.isEmpty(vercode)) {
                return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), "输入的验证码不能为空");
            }
            if (random.equals(inputStr)) {
                return LantResult.ok();
            } else {
                return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), "验证码错误");
            }
        } catch (Exception e) {
            logger.error("===验证码校验失败", e.getMessage());
            return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), "验证码校验失败");
        }
    }

    /**
     * @Title: getSMSVerify
     * @Description: 获取短信验证码
     * @param: request
     * @param: mobile
     * @return: LantResult
     * @author: YFL
     * @date: 2020年5月7日 下午4:02:30
     */
    @RequestMapping(value = "/getSMSVerify")
    public LantResult getSMSVerify(HttpServletRequest request, String mobile) {
        try {
            // 发送短信验证码
            return smsVerifyAO.generateSMSVerificationCode(request, mobile);
        } catch (Exception e) {
            logger.error("===获取短信验证码失败:{}   ", e.getMessage());
            return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), "获取短信验证码失败");
        }
    }

    /**
     * @Title: checkSMSVerify   
     * @Description: 校验短信验证码
     * @param: vercode
     * @param: mobile     
     * @return: LantResult 
     * @author: YFL    
     * @date: 2020年5月20日 上午11:28:42
     */
    @RequestMapping(value = "/checkSMSVerify", method = RequestMethod.POST)
    public LantResult checkSMSVerify(String vercode, String mobile) {
        try {
            return smsVerifyAO.checkSMSVerify(vercode, mobile);
        } catch (Exception e) {
            logger.error("===验证码校验失败", e.getMessage());
            return LantResult.build(ResponseStatusEnum.BAD_REQUEST.getValue(), "验证码校验失败");
        }
    }
}
